package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	"log"
	"os"
)

func main() {
	db, err := gorm.Open("mysql", "root:root@(127.0.0.1)/test?charset=utf8&parseTime=True&loc=Local")
	if err != nil {
		panic(err)
	}
	defer db.Close()
	//日志
	db.LogMode(true)
	db.SetLogger(log.New(os.Stdout, "\r\n", 0))

	//开启事务
	tx := db.Begin()
	stu := new(Student)
	//执行事务
	err = tx.Table("tb_stu").Model(stu).Where("id = ?", 9).Update("name", "张小三").Error

	if err != nil {
		fmt.Println(err)
		//回滚事务
		tx.Rollback()
	}
	//无错误，提交事务
	tx.Commit()
	fmt.Println("提交事务")
}
